package com.hotel.controller.api;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hotel.entity.Function;
import com.hotel.security.model.LoginUser;
import com.hotel.security.model.RedisCache;
import com.hotel.service.function.FunctionService;
import com.hotel.util.JwtUtil;
import com.hotel.util.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RequestMapping("/api")
@RestController
@CrossOrigin
@Slf4j
public class UserMenuController {

    @Resource
    private RedisCache redisCache;

    @Resource
    private FunctionService functionService;

    @PostMapping("/menu")
    public R userMenu(HttpServletRequest request) {
        //解析token
        String token = request.getHeader("token");
        String LoginName = "";
        try {
            LoginName = JwtUtil.getUsername(token);
            //从redis中获取用户信息
            String redisKey = LoginName;
            JSONObject jsonObject = redisCache.getCacheObject(redisKey);
            LoginUser loginUser = jsonObject.parseObject(jsonObject.toJSONString(), LoginUser.class);
            return new R(R.OK, loginUser.getPermissions());
        } catch (Exception e) {
            return new R(R.FORBIDDEN, "菜单加载失败，请稍后再试！");
        }
    }
}
